package com.etime.perfectschool.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.perfectschool.mapper.ActiviesCommentsMapper;
import com.etime.perfectschool.mapper.ActivitiesPhotoMapper;
import com.etime.perfectschool.mapper.SchoolActiviesMapper;
import com.etime.perfectschool.pojo.*;
import com.etime.perfectschool.service.SchoolActiviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchoolActiviesServiceImpl implements SchoolActiviesService {

    @Autowired
    private SchoolActiviesMapper schoolActiviesMapper;
    @Autowired
    private ActivitiesPhotoMapper activitiesPhotoMapper;
    @Autowired
    private ActiviesCommentsMapper activiesCommentsMapper;

    @Override
    public Page<SchoolActivies> getAllSchoolActivies(int current,int size) {
        Page<SchoolActivies> page=new Page<>(current,size);
        return   schoolActiviesMapper.getAllSchoolActivies(page);
    }

    @Override
    public int addSchoolActivies(SchoolActivies schoolActivies) {
         schoolActiviesMapper.addSchoolActivies(schoolActivies);
        return schoolActivies.getActivityId();
    }

    @Override
    public boolean likeSchoolActivies(SchoolActivies schoolActivies) {
        return schoolActiviesMapper.updateById(schoolActivies)==0?false:true;
    }

    @Override
    public SchoolActivies getSchoolActivies(int activityId) {
        return schoolActiviesMapper.selectById(activityId);
    }

    @Override
    public boolean updateSchoolActivies(SchoolActivies schoolActivies) {
        return schoolActiviesMapper.updateById(schoolActivies)==0?false:true;
    }

    @Override
    public boolean deleteSchoolActivies(int activityId) {
        boolean res=false;
        List<ActiviesComments> activiesComments = activiesCommentsMapper.getAllActiviesComments(activityId);
        List<ActivitiesPhoto> activitiesPhotos = activitiesPhotoMapper.getAllActivitiesPhotoByactivityId(activityId);
        if(activiesComments!=null)
        {
            activiesCommentsMapper.deleteActiviesCommentsByactivityId(activityId);
            if(activitiesPhotos!=null)
            {
                activitiesPhotoMapper.deleteActivitiesPhotoById(activityId);
            }
        }else {
            if(activitiesPhotos!=null)
            {
                activitiesPhotoMapper.deleteActivitiesPhotoById(activityId);
            }
        }
        res= schoolActiviesMapper.deleteById(activityId)==0?false:true;
        return res;
    }
}
